<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8" >
            <a-form-model-item label="模号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chtMoldNum">
              <j-search-select-tag placeholder="请输入模号" v-model="model.chtMoldNum" :dictOptions="dictOptions" @change="selectchange()"></j-search-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="试作次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="trialsNumber">
              <j-search-select-tag  @change="trialsNumberchange()"
                v-model="model.trialsNumber"
                dict="yl_mold_test_num"
                placeholder="请输入试作次数"
              />
              
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="研发项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectName">
              <a-input v-model="model.projectName" placeholder="请输入研发项目名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customer">
              <a-input v-model="model.customer" placeholder="请输入客户" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="PO订单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="poOrder">
              <a-input v-model="model.poOrder" placeholder="请输入PO订单号" ></a-input>
            </a-form-model-item>
          </a-col>
          
          <a-col :span="8" >
            <a-form-model-item label="品名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productName">
              <a-input v-model="model.productName" placeholder="请输入品名" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="试作材料/颜色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="trialMaterials">
              <a-input v-model="model.trialMaterials" placeholder="请输入试作材料/颜色" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="系列" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="series">
              <a-input v-model="model.series" placeholder="请输入系列" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="难度系数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="difficultyFactor">
              <a-input v-model="model.difficultyFactor" placeholder="请输入难度系数" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="模具穴数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldCavityNumber">
              <a-input v-model="model.moldCavityNumber" placeholder="请输入模具穴数" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="开模周期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldOpeningCycle">
              <a-input v-model="model.moldOpeningCycle" placeholder="请输入开模周期" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="单重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="singleWeight">
              <a-input v-model="model.singleWeight" placeholder="请输入单重" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="水口重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nozzleWeight">
              <a-input v-model="model.nozzleWeight" placeholder="请输入水口重" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="热流道形式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hotRunnerType">
              <a-input v-model="model.hotRunnerType" placeholder="请输入热流道形式" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="冷流道形式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coldRunnerType">
              <a-input v-model="model.coldRunnerType" placeholder="请输入冷流道形式" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="原料用量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rawMaterialDosage">
              <a-input v-model="model.rawMaterialDosage" placeholder="请输入原料用量" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="增润剂用量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="humectantAmount">
              <a-input v-model="model.humectantAmount" placeholder="请输入增润剂用量" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="机台" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineTable">
              <a-input v-model="model.machineTable" placeholder="请输入机台" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="吨数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tonnage">
              <a-input v-model="model.tonnage" placeholder="请输入吨数" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="试作数量（模）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="trialQuantity">
              <a-input v-model="model.trialQuantity" placeholder="请输入试作数量（模）" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="钳工担当" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fitterResponsible">
              <a-input v-model="model.fitterResponsible" placeholder="请输入钳工担当" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="项目担当" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectResponsibility">
              <a-input v-model="model.projectResponsibility" placeholder="请输入项目担当" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="模具完成时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldCompletionTime">
              <j-date placeholder="请选择模具完成时间" v-model="model.moldCompletionTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="紧急度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="urgencyDegree">
              <a-input v-model="model.urgencyDegree" placeholder="请输入紧急度" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="计划试作时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="planTrialTime">
              <j-date placeholder="请选择计划试作时间" v-model="model.planTrialTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="实际试作时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="actualTrialTime">
              <j-date placeholder="请选择实际试作时间" v-model="model.actualTrialTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="试作目的" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="trialPurpose">
              <a-textarea v-model="model.trialPurpose" rows="4" placeholder="请输入试作目的" />
            </a-form-model-item>
          </a-col>
          
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="模具试模通知子表" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="moldTrialNoticeTypesettingChildTable.loading"
          :columns="moldTrialNoticeTypesettingChildTable.columns"
          :dataSource="moldTrialNoticeTypesettingChildTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MoldTrialNoticeTypesettingTableForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 13 },
        },
        model:{
          series:'医疗'
        },
        dictOptions:[],
        testres:[],
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['moldTrialNoticeTypesettingChild', ],
        tableKeys:['moldTrialNoticeTypesettingChild', ],
        activeKey: 'moldTrialNoticeTypesettingChild',
        // 模具试模通知子表
        moldTrialNoticeTypesettingChildTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '模具通知主表id',
              key: 'moldTrialNoticeTypesettingId',
              type: FormTypes.hidden,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '试作目的',
              key: 'trialPurpose',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/ch/moldTrialNoticeTypesettingTable/add",
          edit: "/ch/moldTrialNoticeTypesettingTable/edit",
          queryById: "/ch/moldTrialNoticeTypesettingTable/queryById",
          queryAllMoldNum: "ch/ylProjectInfo/queryAllMoldNum",
          queryMapByMoldCode:"/ch/ylProblemCorrectionTable/queryMapByMoldCode",
          queryByMoldCodeAndCount:"/ch/moldTrialNoticeTypesettingTable/queryByMoldCodeAndCount",
          moldTrialNoticeTypesettingChild: {
            list: '/ch/moldTrialNoticeTypesettingTable/queryMoldTrialNoticeTypesettingChildByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      this.queryAllMoldNum();
    },
    methods: {
      queryAllMoldNum(){
        getAction(this.url.queryAllMoldNum).then((res)=>{
              if(res.success){
                this.testres = res.result;
                for(let i = 0;i<this.testres.length;i++){
                  this.dictOptions.push({text:this.testres[i],value:this.testres[i]})
                }
                console.log(this.testres);
              }
            })
      },
      selectchange(){
        getAction(this.url.queryMapByMoldCode,{moldNumber:this.model.chtMoldNum}).then((res)=>{
          if(res.success){
            console.log(res.result);
            this.moldInformation = res.result;
            this.model.customer = this.moldInformation.customerName;
            this.model.productName = this.moldInformation.productName;
            this.model.trialMaterials = this.moldInformation.productNumber;
            this.model.moldCavityNumber = this.moldInformation.moldCavitiesNum;
            this.model.moldOpeningCycle = this.moldInformation.openingAndClosingDieTime;
            this.model.singleWeight = this.moldInformation.partWeight;
            this.model.nozzleWeight = this.moldInformation.nozzleWeight;
            this.model.difficultyFactor = this.moldInformation.moldDifficulty;
            this.model.tonnage = this.moldInformation.machineTonnage
            this.$set(this.model,'fitterResponsible',this.moldInformation.fitterCharge);
          }
        })

        if(this.model.chtMoldNum!='' && this.model.trialsNumber!=''){
          getAction(this.url.queryByMoldCodeAndCount,{chtMoldNum:this.model.chtMoldNum,moldTestNum:this.model.trialsNumber}).then((res)=>{
          if(res.success){
            console.log(res.result + "1111");
            this.$set(this.model,'trialPurpose',res.result);
          }else{
            console.log(res.result + "1111");
            this.$set(this.model,'trialPurpose',res.result);
          }
        })
        }
       },
      trialsNumberchange(){
        if(this.model.chtMoldNum!='' && this.model.trialsNumber!=''){
          getAction(this.url.queryByMoldCodeAndCount,{chtMoldNum:this.model.chtMoldNum,moldTestNum:this.model.trialsNumber}).then((res)=>{
          if(res.success){
            console.log(res.result);
            this.$set(this.model,'trialPurpose',res.result);
          }
          else{
            console.log(res.result + "1111");
            this.$set(this.model,'trialPurpose',res.result);
          }
        })
        }
       },
      addBefore(){
        this.moldTrialNoticeTypesettingChildTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.moldTrialNoticeTypesettingChild.list, params, this.moldTrialNoticeTypesettingChildTable)
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          moldTrialNoticeTypesettingChildList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>